package com.meituan.catering.management.product.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.product.api.http.model.request.SaveProductHttpRequest;
import com.meituan.catering.management.product.api.http.model.request.UpdateProductHttpRequest;
import com.meituan.catering.management.product.biz.model.request.UpdateProductBizRequest;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Believe
 * Date 2022/5/23 15:51
 */
public class UpdateProductBizRequestConverter {
    public static UpdateProductBizRequest toUpdateProductBizRequest(UpdateProductHttpRequest updateProductHttpRequest) {
        if (Objects.isNull(updateProductHttpRequest)) {
            return null;
        }
        UpdateProductBizRequest updateProductBizRequest = new UpdateProductBizRequest();
        updateProductBizRequest.setName(updateProductHttpRequest.getName());
        updateProductBizRequest.setUnitPrice(updateProductHttpRequest.getUnitPrice());
        updateProductBizRequest.setMinSalesQuantity(updateProductHttpRequest.getMinSalesQuantity());
        updateProductBizRequest.setIncreaseSalesQuantity(updateProductHttpRequest.getIncreaseSalesQuantity());
        updateProductBizRequest.setUnitOfMeasure(updateProductHttpRequest.getUnitOfMeasure());
        updateProductBizRequest.setDescription(updateProductHttpRequest.getDescription());
        updateProductBizRequest.setVersion(updateProductHttpRequest.getVersion());
        updateProductBizRequest.setMethodGroups(toCreateMethodGroup(updateProductHttpRequest.getMethodGroups()));
        updateProductBizRequest.setAccessoryGroups(toCreateAccessoryGroup(updateProductHttpRequest.getAccessoryGroups()));
        return updateProductBizRequest;
    }

    public static List<UpdateProductBizRequest.AccessoryGroup> toCreateAccessoryGroup(List<UpdateProductHttpRequest.AccessoryGroup> accessoryGroupList) {
        if (CollectionUtils.isEmpty(accessoryGroupList)) {
            return Collections.emptyList();
        }
        List<UpdateProductBizRequest.AccessoryGroup> accessoryGroups = Lists.newLinkedList();
        for (SaveProductHttpRequest.AccessoryGroup methodGroup : accessoryGroupList) {
            UpdateProductBizRequest.AccessoryGroup accessoryGroup = new UpdateProductBizRequest.AccessoryGroup();
            accessoryGroup.setName(methodGroup.getName());
            accessoryGroup.setOptions(toAccessoryOption(methodGroup.getOptions()));
            accessoryGroups.add(accessoryGroup);
        }
        return accessoryGroups;
    }

    public static List<UpdateProductBizRequest.AccessoryGroup.Option> toAccessoryOption(List<UpdateProductHttpRequest.AccessoryGroup.Option> optionList) {
        if (CollectionUtils.isEmpty(optionList)) {
            return Collections.emptyList();
        }
        List<UpdateProductBizRequest.AccessoryGroup.Option> optionBOList = Lists.newLinkedList();
        for (SaveProductHttpRequest.AccessoryGroup.Option option : optionList) {
            UpdateProductBizRequest.AccessoryGroup.Option optionBiz = new UpdateProductBizRequest.AccessoryGroup.Option();
            optionBiz.setId(option.getId());
            optionBiz.setName(option.getName());
            optionBiz.setUnitPrice(option.getUnitPrice());
            optionBiz.setUnitOfMeasure(option.getUnitOfMeasure());
            optionBOList.add(optionBiz);
        }
        return optionBOList;
    }

    public static List<UpdateProductBizRequest.MethodGroup> toCreateMethodGroup(List<UpdateProductHttpRequest.MethodGroup> methodBOList) {
        if (CollectionUtils.isEmpty(methodBOList)) {
            return Collections.emptyList();
        }
        List<UpdateProductBizRequest.MethodGroup> methodGroupList = Lists.newLinkedList();
        for (SaveProductHttpRequest.MethodGroup methodGroup : methodBOList) {
            UpdateProductBizRequest.MethodGroup methodBizGroup = new UpdateProductBizRequest.MethodGroup();
            methodBizGroup.setName(methodGroup.getName());
            methodBizGroup.setOptions(toMethodOption(methodGroup.getOptions()));
            methodGroupList.add(methodBizGroup);
        }
        return methodGroupList;
    }

    public static List<UpdateProductBizRequest.MethodGroup.Option> toMethodOption(List<UpdateProductHttpRequest.MethodGroup.Option> optionList) {
        if (CollectionUtils.isEmpty(optionList)) {
            return Collections.emptyList();
        }
        List<UpdateProductBizRequest.MethodGroup.Option> optionBOList = Lists.newLinkedList();
        for (SaveProductHttpRequest.MethodGroup.Option option : optionList) {
            UpdateProductBizRequest.MethodGroup.Option optionBiz = new UpdateProductBizRequest.MethodGroup.Option();
            optionBiz.setName(option.getName());
            optionBiz.setId(option.getId());
            optionBOList.add(optionBiz);
        }
        return optionBOList;
    }
}
